package com.jspxcms.core.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jspxcms.core.domain.User;
import com.jspxcms.core.domain.UserDetail;
import com.jspxcms.core.repository.UserDetailDao;
import com.jspxcms.core.service.UserDetailService;

@Service
@Transactional(readOnly = true)
public class UserDetailServiceImpl implements UserDetailService {
    public UserDetail get(Integer id) {
        return dao.findOne(id);
    }

    @Transactional
    public UserDetail save(UserDetail detail, User user, String ip) {
        detail.setUser(user);
        detail.setCreationIp(ip);
        detail.applyDefaultValue();
        detail = dao.save(detail);
        user.setDetail(detail);
        return detail;
    }

    @Transactional
    public UserDetail update(UserDetail bean) {
        bean.applyDefaultValue();
        bean = dao.save(bean);
        return bean;
    }

    @Override
    public boolean updateScore(Integer id, Integer score) {
        return dao.updateScore(id, score) > 0;
    }

    @Override
    public boolean updateScore(Integer userId, Integer type, Integer score) {
        if (type == 1) { //收藏
            return dao.updateScoreFavorite(userId, score) > 0;
        }
        if (type == 2) { //学习
            return dao.updateScoreStudy(userId, score) > 0;
        }
        return false;
    }

    @Override
    public void updateHospital(Integer userId, String hospital) {
       dao.updateHospital(userId,hospital);
    }

    private UserDetailDao dao;

    @Autowired
    public void setDao(UserDetailDao dao) {
        this.dao = dao;
    }
}
